package com.grey_zoo.lottery.presenter;

import java.util.ArrayList;
import java.util.List;

import ZhongXinFenPai.TCallback;
import ZhongXinFenPai.TEvent;
import ZhongXinFenPai.TIntent;
import ZhongXinFenPai.TObserver;
import android.widget.Toast;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

import com.grey_zoo.lottery.MainActivity;
import com.grey_zoo.lottery.bean.BmobLotteryData;
import com.grey_zoo.lottery.bean.BmobUserData;
import com.grey_zoo.lottery.event.BombEvent;
import com.grey_zoo.lottery.eventhandler.ChnageUIEventHandler;
import com.grey_zoo.lottery.global.GlobalValue;
import com.grey_zoo.lottery.util.ConfigSharePreferenceUtils;
import com.grey_zoo.lottery.util.JSONUtils;
import com.grey_zoo.lottery.util.ShopingCar;

public class BombEventManager implements TObserver {

	public int chongzhimoney;
	public int buymoney;
	public MyLotteryCheck lotteryCheck;

	public static BombEventManager bombmanager = new BombEventManager();

	public BombEventManager() {
		RegisterEvent();

	}

	@Override
	public void onEvent(TEvent event, TCallback callback)
			throws NoSuchMethodException, Exception, Exception {
		// TODO Auto-generated method stub
		BombEvent bomb = (BombEvent) event;

		switch (bomb.flag) {
		case 0:
			initbomb();

			break;
		case 1:
			checkAndGetBmobMoney();
			break;
		case 2:

			chongzhi(bomb.chongzhimoney);
			break;
		case 3:

			jianshao(bomb.buymoney);
			break;

		case 4:
			setLotteryData();
			break;
		case 5:
			GetMyLottery(bomb.lotterycheck);
			break;
		case 6:
			GetDetailLottery(bomb.lotterycheck, bomb.qs,
					bomb.time);
			break;
		default:
			break;

		}

	}

	public void RegisterEvent() {

		TIntent.getInstance().registerEvent(BombEvent.class, this);

	}

	public static BombEventManager getInstance() {

		return bombmanager;
	}

	public void initbomb() {

		Bmob.initialize(GlobalValue.activity, GlobalValue.BMOBKEY);
	}

	public int checkAndGetBmobMoney() {
		ConfigSharePreferenceUtils sharePreferenceUtils = new ConfigSharePreferenceUtils(
				GlobalValue.activity);

		final String openid = sharePreferenceUtils.getString("openId", "");

		BmobQuery<BmobUserData> bmobQuery = new BmobQuery<BmobUserData>();
		bmobQuery.addWhereEqualTo("name", openid);
		// bmobQuery.addWhereNotEqualTo("age", 25);
		// bmobQuery.setCachePolicy(CachePolicy.CACHE_ELSE_NETWORK);
		// 先从缓存取数据，如果没有的话，再从网络取。

		bmobQuery.findObjects(GlobalValue.activity,
				new FindListener<BmobUserData>() {

					@Override
					public void onSuccess(List<BmobUserData> arg0) {
						// 成功
						// System.out.println(arg0.get(0).getName()==openid+"88");
						System.out.println(arg0.size() + "pp");

						if (arg0.size() == 0) {

							BmobUserData userData = new BmobUserData();
							userData.setMoney(0);
							userData.setName(openid);
							userData.save(GlobalValue.activity,
									new SaveListener() {

										@Override
										public void onSuccess() {
											// TODO 自动生成的方法存根
											Toast.makeText(
													GlobalValue.activity,
													"创建成功", Toast.LENGTH_SHORT)
													.show();
										}

										@Override
										public void onFailure(int arg0,
												String arg1) {
											// TODO 自动生成的方法存根
											Toast.makeText(
													GlobalValue.activity,
													"创建失败", Toast.LENGTH_SHORT)
													.show();
										}
									});

						} else {
							GlobalValue.userMoney = arg0.get(0).getMoney();
							MainActivity.initSlideMenuFragment();
						}
					}

					@Override
					public void onError(int arg0, String arg1) {
						// 失败

						// System.out.println("ddd");
						BmobUserData userData = new BmobUserData();
						userData.setMoney(0);
						userData.setName(openid);
						userData.save(GlobalValue.activity, new SaveListener() {

							@Override
							public void onSuccess() {
								// TODO 自动生成的方法存根
								Toast.makeText(GlobalValue.activity, "创建成功",
										Toast.LENGTH_SHORT).show();
							}

							@Override
							public void onFailure(int arg0, String arg1) {
								// TODO 自动生成的方法存根
								Toast.makeText(GlobalValue.activity, "创建失败",
										Toast.LENGTH_SHORT).show();
							}
						});

					}
				});

		return 0;
	}

	public void chongzhi(int money) {
		// TODO 自动生成的方法存根
		this.chongzhimoney = money;

		ConfigSharePreferenceUtils sharePreferenceUtils = new ConfigSharePreferenceUtils(
				GlobalValue.activity);

		final String openid = sharePreferenceUtils.getString("openId", "");

		BmobQuery<BmobUserData> bmobQuery = new BmobQuery<BmobUserData>();
		bmobQuery.addWhereEqualTo("name", openid);

		bmobQuery.findObjects(GlobalValue.activity,
				new FindListener<BmobUserData>() {

					@Override
					public void onSuccess(List<BmobUserData> arg0) {
						// 成功
						final BmobUserData userData = arg0.get(0);
						String id = arg0.get(0).getObjectId();
						int orimoney = arg0.get(0).getMoney();

						userData.setMoney(orimoney + chongzhimoney);

						userData.update(GlobalValue.activity,
								new UpdateListener() {

									@Override
									public void onSuccess() {
										// TODO 自动生成的方法存根
										Toast.makeText(GlobalValue.activity,
												"充值成功", Toast.LENGTH_SHORT)
												.show();
										GlobalValue.userMoney = userData
												.getMoney();
										/*
										 * MiddleManager.getInstance().changeUI(
										 * "userdata", null);
										 */
										/*GlobalValue.activity.runOnUiThread(new Runnable() {
											
											@Override
											public void run() {
												// TODO Auto-generated method stub
												ChnageUIEventHandler.SwitchUI("userdata", null);
											}
										});*/
										ChnageUIEventHandler.SwitchUI("userdata", null);
									}

									@Override
									public void onFailure(int arg0, String arg1) {
										// TODO 自动生成的方法存根
										Toast.makeText(GlobalValue.activity,
												"充值失败，请稍后再试~~",
												Toast.LENGTH_SHORT).show();
									}
								});

					}

					@Override
					public void onError(int arg0, String arg1) {
						// 失败
						Toast.makeText(GlobalValue.activity, "充值失败",
								Toast.LENGTH_LONG).show();
					}
				});

	}

	public void jianshao(int money) {
		this.buymoney = money;
		// TODO 自动生成的方法存根
		ConfigSharePreferenceUtils sharePreferenceUtils = new ConfigSharePreferenceUtils(
				GlobalValue.activity);
		final String openid = sharePreferenceUtils.getString("openId", "");

		BmobQuery<BmobUserData> bmobQuery = new BmobQuery<BmobUserData>();
		bmobQuery.addWhereEqualTo("name", openid);

		bmobQuery.findObjects(GlobalValue.activity,
				new FindListener<BmobUserData>() {

					@Override
					public void onSuccess(List<BmobUserData> arg0) {
						// 成功
						final BmobUserData userData = arg0.get(0);
						String id = arg0.get(0).getObjectId();
						int orimoney = arg0.get(0).getMoney();

						userData.setMoney(orimoney - buymoney);

						userData.update(GlobalValue.activity,
								new UpdateListener() {

									@Override
									public void onSuccess() {
										// TODO 自动生成的方法存根
										Toast.makeText(GlobalValue.activity,
												"购买成功", Toast.LENGTH_SHORT)
												.show();
										GlobalValue.userMoney = userData
												.getMoney();
									}

									@Override
									public void onFailure(int arg0, String arg1) {
										// TODO 自动生成的方法存根
										Toast.makeText(GlobalValue.activity,
												"购买失败，请稍后再试~~",
												Toast.LENGTH_SHORT).show();
									}
								});

					}

					@Override
					public void onError(int arg0, String arg1) {
						// 失败

					}
				});
	}

	public void setLotteryData() {
		ConfigSharePreferenceUtils sharePreferenceUtils = new ConfigSharePreferenceUtils(
				GlobalValue.activity);
		final String openid = sharePreferenceUtils.getString("openId", "");

		ArrayList<ArrayList<Integer>> blue = ShopingCar.getInstance()
				.getList_blue();
		ArrayList<ArrayList<Integer>> red = ShopingCar.getInstance()
				.getList_red();

		String strblue = JSONUtils.ALToJson(blue);

		String strred = JSONUtils.ALToJson(red);

		BmobLotteryData lotteryData = new BmobLotteryData();
		lotteryData.setIssue(GlobalValue.SSQQS);
		lotteryData.setOpenid(openid);
		lotteryData.setRed(strred);
		lotteryData.setBlue(strblue);
		lotteryData.save(GlobalValue.activity, new SaveListener() {

			@Override
			public void onSuccess() {
				// TODO 自动生成的方法存根
				// Toast.makeText(GlobalValue.context, "创建成功",
				// Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onFailure(int arg0, String arg1) {
				// TODO 自动生成的方法存根
				System.out.println("创建失败" + arg1);
				// Toast.makeText(GlobalValue.context, "创建失败",
				// Toast.LENGTH_SHORT).show();
			}
		});

	}

	public interface MyLotteryCheck {
		void onSuuccd(List<BmobLotteryData> arg0);

		void onFail();
	}

	public void GetMyLottery(MyLotteryCheck lotteryChec) {
		this.lotteryCheck = lotteryChec;
		ConfigSharePreferenceUtils sharePreferenceUtils = new ConfigSharePreferenceUtils(
				GlobalValue.activity);
		final String openid = sharePreferenceUtils.getString("openId", "");

		BmobQuery<BmobLotteryData> bmobQuery = new BmobQuery<BmobLotteryData>();
		bmobQuery.addWhereEqualTo("openid", openid);

		bmobQuery.findObjects(GlobalValue.activity,
				new FindListener<BmobLotteryData>() {

					@Override
					public void onSuccess(List<BmobLotteryData> arg0) {
						// 成功

						lotteryCheck.onSuuccd(arg0);

					}

					@Override
					public void onError(int arg0, String arg1) {
						// 失败
						lotteryCheck.onFail();
					}
				});
	}

	public void GetDetailLottery(MyLotteryCheck lotteryChec, String qs,
			String time) {
		this.lotteryCheck = lotteryChec;
		ConfigSharePreferenceUtils sharePreferenceUtils = new ConfigSharePreferenceUtils(
				GlobalValue.activity);
		final String openid = sharePreferenceUtils.getString("openId", "");

		BmobQuery<BmobLotteryData> bmobQuery = new BmobQuery<BmobLotteryData>();
		bmobQuery.addWhereEqualTo("openid", openid);
		bmobQuery.addWhereEqualTo("issue", qs);
		bmobQuery.addWhereEqualTo("createAt", time);

		bmobQuery.findObjects(GlobalValue.activity,
				new FindListener<BmobLotteryData>() {

					@Override
					public void onSuccess(List<BmobLotteryData> arg0) {
						// 成功

						lotteryCheck.onSuuccd(arg0);

					}

					@Override
					public void onError(int arg0, String arg1) {
						// 失败
						lotteryCheck.onFail();
					}
				});
	}

}
